package dymnd.ui.menu;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.LinkedList;

import javax.swing.JPanel;

public class DrawerPane extends JPanel{

	
	/**
	 * DrawerPane (the class implementing it, anyways), should add each DrawerMenu to a JPanel to surround it and provide dragging/rendering 
	 * capability. No components should be directly added to the DrawerPane. 
	 * 
	 */
	LinkedList<DrawerContainer> drawers = new LinkedList<DrawerContainer>();
	
	
	public void addDrawer(DrawerMenu drawer)	{
		drawers.addLast(new DrawerContainer(drawer, this));
		add(drawers.getLast());
		drawers.getLast().setHandle(new Handle(10, 30));
		drawers.getLast().setLocation(0, 200);
	}
	boolean handleDragStart = true;
	int lastx;
	int lasty;
	protected void handleDragged(DrawerContainer dc, int x, int y)	{
		if(handleDragStart)	{
			lastx = x;
			lasty = y;
		}
		
		int newx = dc.getX() - (lastx - x);
		int newy = dc.getY() - (lasty - y);
		
		dc.setLocation(newx, newy);
		
		
		lastx = x;
		lasty = y;
		handleDragStart = false;
	}
	
	public void paintComponent(Graphics g)	{
		
	}
	
	protected void handleReleased(DrawerContainer dc)	{
		handleDragStart = true;
	}
	
	
	//should decide what to do when a drawer is closed, including setting it invisible and making sure it no longer reacts with other
	//DrawerMenus, as well as notifying the overlying controller that the drawer has been closed.
	public void drawerClosed(DrawerMenu m)	{
		
	}
}


class DrawerContainer extends JPanel	{
	
	private Container drawerContents;
	
	private int handleWidth = 10;
	private int handleHeight = 20;
	private Handle handle;
	private DrawerPane pane;
	public DrawerContainer(Container c, DrawerPane p)	{
		drawerContents = c;
		pane = p;
		setSize(c.getWidth()+ handleWidth, c.getHeight());
		
		Handle handle = new Handle(handleWidth, handleHeight);
		setHandle(handle);
		
	}
	
	private boolean flatState = false;
	
	private void setFlatState(boolean b)	{
		flatState = b;
		if(b)
			flatten();
		else
			unflatten();
		
	}
	
	Image backg;
	private void flatten()	{
		if(backg == null)
			backg = createImage(drawerContents.getWidth(), drawerContents.getHeight());
		
		drawerContents.paintAll(backg.getGraphics());
		drawerContents.setVisible(false);
		
	}
	
	private void unflatten()	{
		drawerContents.setVisible(true);
	}
	
	public void paintComponent(Graphics g)	{
		if(!flatState)
			super.paintComponent(g);
		else	{
			g.drawImage(backg, 0, 0, this);
		}
	}
	
	public void setHandle(Handle h)	{
		//remove the previous handle instance
		//remove(handle);
		
		handle = h;
		handle.setLocation(getWidth() - handleWidth, getHeight()/2 - handleHeight/2);
		handle.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e)	{
				handle.onPressed();
			}
			public void mouseReleased(MouseEvent e)	{
				handle.onReleased();
				onReleased();
			}
		});
		
		handle.addMouseMotionListener(new MouseMotionAdapter()	{
			public void mouseDragged(MouseEvent e)	{
				onDragged(e.getXOnScreen(), e.getYOnScreen());
			}
		});
		
		
		add(handle);
	}
	
	private void onReleased()	{
		pane.handleReleased(this);
	}
	private void onDragged(int x, int  y)	{
		pane.handleDragged(this, x, y);
	}
	
	public void close()	{
		
	}
	
}

class Handle extends JPanel	{
	private DrawerContainer container;
	
	public static final int SIDE_EAST = 0;
	public static final int SIDE_WEST = 1;
	
	private int side = SIDE_WEST;
	private Image leftPointingIcon;
	private Image rightPointingIcon;
	private boolean isPressed = false;
	
	private boolean isOpen = false;
	
	public Handle(int w, int h)	{
		setSize(w, h);
		leftPointingIcon = Toolkit.getDefaultToolkit().getImage("resources/ui/left-pointing-arrow.png");
		rightPointingIcon  = Toolkit.getDefaultToolkit().getImage("resources/ui/right-pointing-arrow.png");
		
	}
	
	public void setOpen(boolean b)	{
		isOpen = b;
	}
	
	public void setWindowSide(int side)	{
		this.side = side;
	}
	Image backg;
	public void paintComponent(Graphics arg0)	{
		
		if(backg == null || backg.getWidth(this) != getWidth() || backg.getHeight(this) != getHeight())
			backg = createImage(getWidth(), getHeight());
		Graphics g = backg.getGraphics();
		g.setColor(getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
		
		if(isPressed)	{
			g.setColor(new Color(255, 128, 0, 40));
			g.fillRect(0, 0, getWidth(), getHeight());
		}
		
		//draw the arrow
		if((side == SIDE_WEST && isOpen) || (side == SIDE_EAST && !isOpen))	
			drawArrowLeft(g);
		else
			drawArrowRight(g);
		
		arg0.drawImage(backg, 0, 0, this);
		g.dispose();
		arg0.dispose();
	}
		
	private void drawArrowLeft(Graphics g)	{
		g.drawImage(leftPointingIcon, getWidth()/2 - leftPointingIcon.getWidth(this)/2, getHeight()/2 - leftPointingIcon.getHeight(this)/2, this);
	}
	
	private void drawArrowRight(Graphics g)	{
		g.drawImage(rightPointingIcon, getWidth()/2 - rightPointingIcon.getWidth(this)/2, getHeight()/2 - rightPointingIcon.getHeight(this)/2, this);
	}
	
	public void onPressed()	{
		isPressed = true;
		repaint();
	}
	
	public void onReleased()	{
		isPressed = false;
		repaint();
	}
}